//
//  CYBettingHeaderView.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/7/15.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYBettingHeaderView.h"
@interface CYBettingHeaderView(){
    
    BettingBlock _myblock;
}
@property (nonatomic, strong) CYButton  *yaoyiyaoBtn;
@property (nonatomic, strong) CYButton *shoppingBtn;
@property (nonatomic, strong) CYLabel *shopNumLBL;
@end
@implementation CYBettingHeaderView
- (instancetype)initWithColor:(UIColor *)color block:(BettingBlock)block{
    self = [super initWithColor:color];
    if (self) {
        _myblock = block;
        self.yaoyiyaoBtn.hidden = self.shoppingBtn.hidden = NO;
    }
    return self;
}

- (CYButton *)yaoyiyaoBtn{
    if (!_yaoyiyaoBtn) {
        _yaoyiyaoBtn = [[CYButton alloc]initWithTitle:@"摇一摇机选" titleColor:ColorHex(0x888888) titleSize:AL(PT(30)) Image:@"yaoyiyaoicon"];
        _yaoyiyaoBtn.titleEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0);
        _yaoyiyaoBtn.imageEdgeInsets = UIEdgeInsetsMake(0.0, -10.0, 0.0, 0.0);
        [_yaoyiyaoBtn addTarget:self action:@selector(actionForYaoYiYaoBtn) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_yaoyiyaoBtn];
        
        CGSize size = [_yaoyiyaoBtn.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:_yaoyiyaoBtn.titleLabel.font}];
        [_yaoyiyaoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(AL(PT(28)));
            make.top.equalTo(self).with.offset(AL(PT(20)));
            make.size.mas_equalTo(CGSizeMake(size.width + _yaoyiyaoBtn.imageView.bounds.size.width + 10, MAX(_yaoyiyaoBtn.titleLabel.bounds.size.height, _yaoyiyaoBtn.imageView.bounds.size.height)));
        }];
    }
    return _yaoyiyaoBtn;
}

- (CYButton *)shoppingBtn{
    if (!_shoppingBtn) {
        _shoppingBtn = [[CYButton alloc]initWithTitle:@"购物车" titleColor:ColorHex(0x888888) titleSize:AL(PT(30)) Image:@"ShoppingCart"];
        [_shoppingBtn addTarget:self action:@selector(actionForShoppingBtn) forControlEvents:UIControlEventTouchUpInside];
        _shoppingBtn.titleEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0);
        _shoppingBtn.imageEdgeInsets = UIEdgeInsetsMake(0.0, -10.0, 0.0, 0.0);
        [self addSubview:_shoppingBtn];
        
        CGSize size = [_shoppingBtn.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:_shoppingBtn.titleLabel.font}];
        [_shoppingBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.mas_right).with.offset(- AL(PT(28)));
            make.top.equalTo(_yaoyiyaoBtn);
            make.size.mas_equalTo(CGSizeMake(size.width + _shoppingBtn.imageView.bounds.size.width + 10, MAX(_shoppingBtn.titleLabel.bounds.size.height, _shoppingBtn.imageView.bounds.size.height)));
        }];
    }
    return _shoppingBtn;
    
}

- (CYLabel *)shopNumLBL{
    if (!_shopNumLBL) {
        _shopNumLBL = [[CYLabel alloc]initWithText:nil size:AL(PT(20)) textcolor:[UIColor whiteColor]];
        CGFloat radius = MAX(_shopNumLBL.bounds.size.width, _shopNumLBL.bounds.size.height) * 1.5;
        _shopNumLBL.textAlignment = NSTextAlignmentCenter;
        _shopNumLBL.layer.cornerRadius = radius / 2.0;
        _shopNumLBL.layer.borderWidth = 0.0;
        _shopNumLBL.layer.masksToBounds = YES;
        _shopNumLBL.backgroundColor = THEME_COLOR;
        [self addSubview:_shopNumLBL];
        
        [_shopNumLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_shoppingBtn).with.offset(- radius / 2.0);
            make.left.equalTo(_shoppingBtn.mas_right).with.offset( - radius / 2.0);
            make.size.mas_equalTo(CGSizeMake(radius, radius));
        }];
        
    }
    return _shopNumLBL;
}
- (void)setShopNum:(int)shopNum{
    if (_shopNum != shopNum){
        _shopNum = shopNum;
        self.shopNumLBL.hidden = (shopNum > 0)?NO:YES;
        self.shopNumLBL.text = [NSString stringWithFormat:@"%d", shopNum];
        [self updateBoundsOfShopNumLBL];

    }
}

- (void)updateBoundsOfShopNumLBL{
    
    self.shopNumLBL.text = (_shopNum <= 99)?[NSString stringWithFormat:@"%d", _shopNum]:[NSString stringWithFormat:@"..."];
    
    CGFloat radius = MAX(_shopNumLBL.bounds.size.width, _shopNumLBL.bounds.size.height) * 1.5;
    _shopNumLBL.layer.cornerRadius = radius / 2.0;
    [_shopNumLBL mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_shoppingBtn).with.offset(- radius / 2.0);
        make.left.equalTo(_shoppingBtn.mas_right).with.offset( - radius / 2.0);
        make.size.mas_equalTo(CGSizeMake(radius, radius));
    }];
    
}

- (void)actionForYaoYiYaoBtn{
    _myblock(@"摇一摇");
}

- (void)actionForShoppingBtn{
    
    _myblock(@"购彩车");
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
